<template>
  <div class="boxs">
    <div class="flex-box">
      <div class="top">
    <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
    >
      <el-form-item label="" prop="name">
        <el-input
            v-model="queryParams.query"
            placeholder="请输入单位名称"
            clearable
            size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
        >搜索</el-button>
      </el-form-item>
    </el-form>
      </div>
      <div>
    <el-button icon="el-icon-plus" type="primary" plain size="small" @click="openDialog">添加单位</el-button>
      </div>
    </div>

    <el-table :data="UnitArr" style="width: 100%" v-loading="loading">
      <el-table-column label="编号" align="center" prop="id" min-width="80">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="center" prop="name" min-width="100">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" min-width="180" :formatter="formatterCreateTime" />
      <el-table-column label="操作" align="center" min-width="180" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini"  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini"  type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.size"
        @pagination="getUnit"
    />
    <el-dialog title="单位信息" :visible.sync="dialogVisible" width="40%"  :close-on-click-modal="false">
      <el-form class="demo-form-inline" label-width="80px" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="单位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入单位名称" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" v-loading="addLoading">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :type="form.id ? 'warning' : 'primary'" @click="affirm">{{
            form.id ? "确认修改" : "确认添加"
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getUnit, addUnit, upUnit, delUnit} from '@/api/admin';
import {formatDate} from "@/utils";
export default {
  name:"Unit",
  data () {
    return {
      queryParams:{
        page:1,
        size:10
      },
      total:0,
      loading:false,
      addLoading:false,
      UnitArr: [],
      dialogVisible: false,
      rolesArr: [],
      moreArr: [],
      search: '',
      form: {
      },
      rules: {
        name: [
          { required: true, message: '请输入单位名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
      },
    }
  },
  async created () {
    this.getUnit()
  },
  methods: {
    formatterCreateTime(row){
      return formatDate(row.updatedTime);
    },

    handleDelete (id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        await delUnit({ id: id });
        this.getUnit();
        this.$message({
          message: '删除成功！',
          type: 'success'
        });
      })
    },
    resetForm () {
      this.form = {status:1};
    },
    handleEdit (index, row) {
      this.resetForm();
      this.form = {...row};
      this.dialogVisible = true;
    },
    async affirm () {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (!valid) return;
        try {
          this.addLoading=true;
          !this.form.id&& await addUnit(this.form);
          this.form.id&& await upUnit(this.form);
          this.addLoading=false;
          this.$message.success(this.form.id?'修改成功！':'添加成功！');
          this.getUnit();
          this.dialogVisible = false;
        }catch (e) {
          this.addLoading=false;
        }

      });
    },
    openDialog () {
      this.resetForm();
      this.dialogVisible = true
    },
    async getUnit () {
      this.loading=true;
      const {data,total} = await getUnit(this.queryParams);
      this.loading=false;
      this.UnitArr = data;
      this.total=total;
    },
    handleQuery(){
      this.queryParams.page=1;
      this.getUnit();
    }
  }
}
</script>
<style scoped lang="scss">
.top .el-form-item{
  margin-bottom: 0;
}
.boxs {
  padding: 20px;
}
.flex-box{
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
.color-title{
  margin-bottom: 10px;
  color: #000;
}
</style>
